<?php
/**
 * 房源管理
 */
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
load()->func('tpl');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';	
$selects = $this->_forms;
$uniacid = $_W['uniacid']; 
$openid = $_W['openid'];
$sec = $this->module_setting();
/*判断操作权限 公众号管理员*/
$opersql = '';
if($_W['user']['type']==3){    
    $opersql = " AND `uid` =:uid ";
}
 
function select_city(){
    global $_W,$_GPC;    
    $sql = "select * from ". tablename("kbwdl_area")." where uniacid='".$_W['uniacid']."' and type='4' and disabled=0" ; 
    $all = pdo_fetchall($sql );
    $tmp = array();
    foreach($all as $key=>$val){
        $tmp[$val['id']] = $val['name'];
    }
    return $tmp;
}

    
if($operation =='display'){
    /*如果有ids 表示移动到栏目*/
    if(isset($_GPC['ids']) && !empty( $_GPC['ids'] )){
        $sk = intval($_GPC['movhtype']);
        $sqladd_tmp = array(
            '',
            '  rent_type=0 , zhutype=0 ',
            '  rent_type=2 , zhutype=0 ',
            '  rent_type=0 , zhutype=1 ',
            '  rent_type=2 , zhutype=1 ',
            '  rent_type=0 , zhutype=2 ',
            '  rent_type=2 , zhutype=2 ',
            '  rent_type=0 , zhutype=3 ',
             '  zhutype=7 ',
              '  zhutype=8 ',
        );
        if(!empty($sqladd_tmp[$sk])){
            $ids = implode(",", $_GPC['ids']);
            pdo_query("update ".tablename('kbwdl_house'). " set ". $sqladd_tmp[$sk]." where uniacid='$uniacid' and id in ($ids) ");
        }
    }

    /*移动房源结束*/
    $pindex = max(1, intval($_GPC['page']));
    $psize = 15;
    $condition = ' WHERE `uniacid` = :uniacid  ';
    $params = array(':uniacid' => $_W['uniacid']);
    if($opersql){
        $condition .= $opersql;
        $params[':uid'] = $_W['uid'];
    }
    if (!empty($_GPC['keyword'])) {
            $condition .= ' AND (`title` LIKE :title OR `village_name` LIKE :title OR `area` LIKE :title OR `house_sno` LIKE :title OR `publish_name`  LIKE :title OR `linkphone` LIKE :title )';
            $params[':title'] = '%' . trim($_GPC['keyword']) . '%';
    }
   if (isset($_GPC['isdelete']) &&  $_GPC['isdelete'] ==1) {
            $condition .= ' AND `isdelete` = :isdelete ';
            $params[':isdelete'] = 1;
    }
    /*查询指定经纪人的*/
     if (isset($_GPC['bid']) && !empty($_GPC['bid'])  ) {
            $condition .= ' AND `broker_id` = :broker_id ';
            $params[':broker_id'] = intval($_GPC['bid']);
    }
    if (isset($_GPC['isonline']) &&  $_GPC['isonline'] ==1) {
            $condition .= ' AND `isonline` = :isonline ';
            $params[':isonline'] = 0;
    }
    if(!empty($_GPC['qzg'])){
         $condition .= ' AND `zhutype` = 4 ';
    }else{
        $condition .= ' AND `zhutype` <> 4 ';
    }
    /*
     *城市区域搜索     * 
     */
    if(isset($_GPC['data']['city_id']) && !empty($_GPC['data']['city_id']) ){
        $condition .= ' AND `city_id` = :city_id';
            $params[':city_id'] = intval($_GPC['data']['city_id']);
            
    }
    if(isset($_GPC['data']['area']) && !empty($_GPC['data']['area']) ){
        $condition .= ' AND `area` = :area';
            $params[':area'] = trim($_GPC['data']['area']);
    }
    /**
     * 按类型搜索
     */
    if(isset($_GPC['htype']) && $_GPC['htype']!=''){
        $sqladd_tmp = array(
            '',
            ' AND rent_type=0 AND zhutype=0 ',
            ' AND rent_type=2 AND zhutype=0 ',
            ' AND rent_type=0 AND zhutype=1 ',
            ' AND rent_type=2 AND zhutype=1 ',
            ' AND rent_type=0 AND zhutype=2 ',
            ' AND rent_type=2 AND zhutype=2 ',
            ' AND rent_type=0 AND zhutype=3 ',
            '   AND zhutype=7 ',
             '   AND zhutype=8 ',
        );
        $sk = intval($_GPC['htype']);
        if(!empty($sqladd_tmp[$sk])){
            $condition.= $sqladd_tmp[$sk];
        }
    }else{
        $_GPC['htype'] = '';        
    }

    $sql = 'SELECT COUNT(*) FROM ' . tablename('kbwdl_house') . $condition;
    $total = pdo_fetchcolumn($sql, $params);
    if (!empty($total)) {
            $sql = 'SELECT * FROM ' . tablename('kbwdl_house') . $condition . ' ORDER BY `id` DESC, `endtime` DESC,
                            `id` DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize;
            $list = pdo_fetchall($sql, $params);
            $pager = pagination($total, $pindex, $psize);
    }
    
    /*房源选择类型*/
    $htype_option = array( '全部','二手房','租房','写字楼出售','写字楼出租','商铺出售','商铺出租','生意转让','厂房租售','仓库租售');
    if($sec['isduocity']==1){
        $citys = select_city();
    } 
    
        /*区域商圈*/
    $sitedata['areas'] = form_element::_option($sec['areas']);
    /* 查询商圈 */
    $quan = pdo_fetchall("select * from " . tablename("kbwdl_area") . " where uniacid='$uniacid' and type=2  and disabled=0 order by orderid desc ,id desc");
    if (!empty($quan)) {
        $tmp = array();
        foreach ($quan as $key => $q) {
            $tmp[$q['area']][] = $q['name'];
        }
        foreach ($sitedata['areas'] as $k => $pv) {
            $sitedata['quan'][$k] = $tmp[$pv];
        }
    }
    
    include $this->template('house_list');
    /*
     * 设置房源属性
     */
}elseif($operation=='setproperty'){
    
    $id = intval($_GPC['id']);
    $type = $_GPC['type'];
    $data = intval($_GPC['data']);
    if (in_array($type, array('istop', 'isjingpin', 'ishot', 'show_jiaji','show_you','show_hight'))) {
            $data = ($data==1?'0':'1');
            pdo_update("kbwdl_house", array(  $type => $data), array("id" => $id, "uniacid" => $_W['uniacid']));
            die(json_encode(array("result" => 1, "data" => $data)));
    }
    if (in_array($type, array('isonline'))) {
            $data = ($data==1?'0':'1');
            pdo_update("kbwdl_house", array($type => $data), array("id" => $id, "uniacid" => $_W['uniacid']));
            die(json_encode(array("result" => 1, "data" => $data)));
    }
    if (in_array($type, array('isdelete'))) {
            $data = ($data==1?'0':'1'); 
            pdo_update("kbwdl_house", array($type => $data), array("id" => $id, "uniacid" => $_W['uniacid']));
            die(json_encode(array("result" => 1, "data" => $data)));
    }
    die(json_encode(array("result" => 0)));
}


